package com.aids_malaria.ui.report;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.aids_malaria.R;
import com.aids_malaria.adapter.MosquitoBorneAdapter;
import com.aids_malaria.app.Constant;
import com.aids_malaria.base.BaseActivity;
import com.aids_malaria.http.response.AreaC;
import com.aids_malaria.event.Events;
import com.aids_malaria.http.BaseObserver;
import com.aids_malaria.http.RetrofitFactory;
import com.aids_malaria.http.RxScheduler;
import com.aids_malaria.http.api.RetrofitApi;
import com.aids_malaria.http.response.MosquitoData;
import com.aids_malaria.ui.datacollect.CountySelectActivity;
import com.aids_malaria.util.AuthUtil;
import com.aids_malaria.util.DateUtil;
import com.aids_malaria.util.ToastUtil;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MosquitoBorneActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refresh_layout;
    @BindView(R.id.rv_data)
    RecyclerView rv_data;
    @BindView(R.id.et_county_name)
    TextView et_county_name;

    @BindView(R.id.et_start_time)
    TextView et_start_time;
    @BindView(R.id.et_end_time)
    TextView et_end_time;
    @BindView(R.id.et_density)
    EditText et_density;
    @BindView(R.id.et_positive_rate)
    EditText et_positive_rate;
    @BindView(R.id.et_bite_rate)
    EditText et_bite_rate;
    @BindView(R.id.cb_anopheles)
    CheckBox cb_anopheles;
    @BindView(R.id.cb_culex)
    CheckBox cb_culex;
    @BindView(R.id.cb_aedes)
    CheckBox cb_aedes;

    private MosquitoBorneAdapter mosquitoBorneAdapter;
    private List<MosquitoData> mData = new ArrayList<>();

    private String county_code = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mosquito_borne;
    }

    @Override
    protected void initView() {
        super.initView();
        tv_title.setText(getString(R.string.wenmei_title));
        initRefreshLayout();
    }

    @Override
    protected void initData() {
        super.initData();
        initRecycleView();

//        getMosquitoDat();
    }

    @OnClick({R.id.ll_back, R.id.iv_search,R.id.iv_county_select,R.id.iv_start_time_select,R.id.iv_end_time_select,R.id.et_county_name,R.id.et_end_time
    ,R.id.et_start_time})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.iv_search:

                getMosquitoDat();
                break;
            case R.id.iv_county_select:
            case R.id.et_county_name:
                startActivityForResult(CountySelectActivity.class,0,null);
                break;
            case R.id.iv_start_time_select:
            case R.id.et_start_time:
                DateUtil.newTimeSelect(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_start_time.setText(DateUtil.getFullTimeFormatter().format(date));
//                        refresh_layout.autoRefresh();
                    }
                }).show();
                break;
            case R.id.iv_end_time_select:
            case R.id.et_end_time:
                DateUtil.newTimeSelect(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        et_end_time.setText(DateUtil.getFullTimeFormatter().format(date));
//                        refresh_layout.autoRefresh();
                    }
                }).show();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 201){
//            refresh_layout.autoRefresh();
        }

    }

    /**
     * 县选择回调
     *
     * @param countySelectEvent
     */
    @Subscribe
    public void onCodeEvent(Events.CountySelectEvent countySelectEvent) {
        AreaC areaC = countySelectEvent.getAreaC();
        county_code = areaC.getCounty_code();
        et_county_name.setText(areaC.getCounty_name());
    }

    private void initRefreshLayout() {
        refresh_layout.setEnableLoadMore(false);
        refresh_layout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refresh_layout.finishLoadMore(1500);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refresh_layout.finishRefresh(1000);
                getMosquitoDat();
            }
        });
    }

    private void initRecycleView() {
        cb_anopheles.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                refresh_layout.autoRefresh();
            }
        });
        cb_culex.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                refresh_layout.autoRefresh();
            }
        });
        cb_aedes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                refresh_layout.autoRefresh();
            }
        });
        rv_data.setLayoutManager(new LinearLayoutManager(mContext));
        mosquitoBorneAdapter = new MosquitoBorneAdapter(mContext, R.layout.item_mosquito_borne, mData);
        rv_data.setAdapter(mosquitoBorneAdapter);
    }

    public void getMosquitoDat() {
        HashMap<String, String> map = new HashMap<>();
        map.put("area_code", county_code);
        map.put("invest_time_fr", et_start_time.getText().toString().trim());
        map.put("invest_time_to", et_end_time.getText().toString().trim());
        map.put("density", et_density.getText().toString().trim());
        map.put("infect_rate", et_positive_rate.getText().toString().trim());
        map.put("bit_rate", et_bite_rate.getText().toString().trim());
        map.put("is_culex", cb_culex.isChecked() ? "1" : "0");
        map.put("is_anopheles", cb_anopheles.isChecked() ? "1" : "0");
        map.put("is_aedes", cb_aedes.isChecked() ? "1" : "0");
        map.put("is_other", "1");
        RetrofitFactory.getInstance().createApi(RetrofitApi.class).getMosquitoSum(map)
                .compose(RxScheduler.rxSchedulerTransform())
                .subscribe(new BaseObserver<List<MosquitoData>>() {

                    @Override
                    protected void onStart() {
                        getLoadingDialog().show();
                    }

                    @Override
                    protected void onSuccess(List<MosquitoData> data) throws Exception {
                        if (data != null) {
                            mData.clear();
                            mData.addAll(data);
                            mosquitoBorneAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onFinish() {
                        getLoadingDialog().dismiss();
                    }
                });
    }

}
